using System;
using DbEntities.Entities;
using NHibernate;
using NHibernate.Criterion;

namespace DataLayer.Repositories
{
    public class SubscriptionsRepository : BaseRepository<Subscription, long>, ISubscriptionsRepository
    {
        public SubscriptionsRepository(ISession session)
            : base(session)
        {
        }

        public Subscription Find(Subscriber subscriber, Publisher publisher, String messageType,
                                 MessageAssembly messageAssembly)
        {
            return Session.CreateCriteria(typeof (Subscription))
                .Add(Restrictions.Eq("SubscribedBy", subscriber))
                .Add(Restrictions.Eq("PublishedBy", publisher))
                .Add(Restrictions.Eq("MessageType", messageType))
                .Add(Restrictions.Eq("MessageAssembly", messageAssembly))
                .UniqueResult<Subscription>();
        }
    }
}